﻿Imports System.Reflection
Imports System.Reflection.Emit
Imports VBParser

Namespace Compiler.Semantic

    Public Class NamespaceDescription
        Inherits CompileUnitElementDescription

        Private m_childs As BindingTable(Of NameContainer)
        Private m_types As BindingTable(Of TypeDescription)
        Private m_subNamespaces As BindingTable(Of NamespaceDescription)
        Private m_specialBinder As Binder = Nothing

        Public ReadOnly Property IsTopLevelNamespace() As Boolean
            Get
                Return TypeOf Parent Is CompileUnitDescription
            End Get
        End Property

        Public Sub New(ByVal parent As NameContainer)
            MyBase.New(parent)

            m_childs = New BindingTable(Of NameContainer)
            m_types = New BindingTable(Of TypeDescription)
            m_subNamespaces = New BindingTable(Of NamespaceDescription)

        End Sub

        Friend Sub New(ByVal parent As NameContainer, ByVal specialBinder As Binder)
            MyClass.New(parent)
            m_specialBinder = specialBinder
        End Sub

        Public Overrides ReadOnly Property IdentifyBinder() As Binder
            Get
                If m_specialBinder IsNot Nothing Then
                    Return m_specialBinder
                End If
                Return MyBase.IdentifyBinder
            End Get
        End Property

        Public Overrides ReadOnly Property QualifiedName() As Qualifier
            Get
                Dim qb As New QualifierBuilder()
                If TypeOf Parent Is CompileUnitDescription Then
                    qb.PushTopLevelSection(Name)
                Else
                    qb.AppendQualifier(Parent.QualifiedName)
                    qb.PushNamespaceSection(Name)
                End If

                Return qb.BuildQualifier()
            End Get
        End Property

        Public Function SearchType(ByVal childBinder As Binder) As TypeDescription
            Return m_types.LookUp(childBinder)
        End Function

        Public Function SearchSubNamespace(ByVal childBinder As Binder) As NamespaceDescription
            Return m_subNamespaces.LookUp(childBinder)
        End Function

        'Public Function SearchNamespace(ByVal nsBinder As Binder) As NamespaceDescription
        '    Dim symbolBinder = TryCast(nsBinder, SymbolBinder)
        '    Dim qualifierBinder = TryCast(nsBinder, QualifierBinder)

        '    If symbolBinder IsNot Nothing Then
        '        Dim qualifiedSimpleName As New QualifierBuilder
        '        qualifiedSimpleName.AppendQualifier(Me.QualifiedName)
        '        qualifiedSimpleName.PushNamespaceSection(symbolBinder.Symbol)

        '        Dim newBinder As New QualifierBinder(qualifiedSimpleName.BuildQualifier())

        '        Return CompileUnit.SearchNamespace(newBinder)
        '    ElseIf qualifierBinder IsNot Nothing Then
        '        Dim qualifiedFullName As New QualifierBuilder
        '        qualifiedFullName.AppendQualifier(Me.QualifiedName)
        '        qualifiedFullName.AppendQualifier(qualifierBinder.Qualifier, QualifierSection.NamespaceConnector)

        '        Dim newBinder As New QualifierBinder(qualifiedFullName.BuildQualifier())

        '        Return CompileUnit.SearchNamespace(newBinder)
        '    Else
        '        Return Nothing
        '    End If
        'End Function

        Public Overrides Function SearchChild(ByVal childBinder As Binder) As NameContainer
            Return m_childs.LookUp(childBinder)
        End Function

    End Class
End Namespace